<?php

namespace app\api\controller;

use app\common\model\AdminModel;

use app\common\model\worker\TaskModel;
use think\facade\Db;

class AdminController extends Base
{
    private $model;

    /**
     * 构造方法
     * @access public
     * @throws \Exception
     */
    public function __construct()
    {
        parent::__construct();
        $this->checkLogin();
        $this->model = new AdminModel();

    }

    public function index(){
        $pageSize      = $this->request->param('pageSize/d', 20); //每页多少条
        $pageNumber    = $this->request->param('pageNumber/d'); //当前页
        $sortName      = $this->request->param('sortName'); //排序字段
        $sortOrder     = $this->request->param('sortOrder', 'desc'); //升序降序
        $order         = !empty($sortName) ? "{$sortName} {$sortOrder}" : ''; //排序
        $searchData    = $this->getSearchData();
        $data          = ['total' => 0, 'rows' => []];
        $data['total'] = $this->model->withSearch('filter', $searchData)->count();
        $data['sql'] = $this->model->getLastSql();
        if ($data['total'] > 0) {
            $rows = $this->model->withSearch('filter', $searchData)->page($pageNumber, $pageSize)
                ->order($order)->select()->withoutField('password')->toArray();
            $rows = $this->model->_formatList($rows);
            $data['rows'] = $rows;
        }
        $this->result($data);
    }


    /**
     * 获取员工数据
     * 根据角色选人
     */
    public function selectAdmin(){

    }

    /**
     * 获取用户统计数量
     */
    public function count(){
        $userInfo['customer_total'] = D('worker.customer')->alias('t1')
            ->join('task t2','t1.id=t2.customer_id')
            ->where('receive_admin_id',$this->loginInfo['id'])
            ->group('customer_id')
            ->count();
        $userInfo['order_total'] = D('worker.order')->alias('t1')
            ->join('task t2','t1.id=t2.order_id')
            ->where('receive_admin_id',$this->loginInfo['id'])
            ->count();
        $userInfo['assign_my'] = TaskModel::where('receive_admin_id',$this->loginInfo['id'])->where('is_submit',0)->where('is_receive',1)->count(); //指派给我的
        $userInfo['my_task'] = TaskModel::where('audit_admin_id',$this->loginInfo['id'])->where('is_audit',1)->count(); //我指派的
        $userInfo['total_mount'] =
        $this->result($userInfo);
    }

    public function getInfo(){
        $info = $this->model->where('id',$this->loginInfo['id'])->find();
        $this->result($info);
    }

    /**
     * 获取管理者列表
     */
    public function getSuper(){
        $list = $this->model->where('is_super',1)->select()->toArray();
        $this->result($list);
    }

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author bxm
     * @date 2022-06-24 11:42
     * 员工列表
     */
    public function list(){
        $role_id      = $this->request->param('role_id/d'); //角色id
        $query = [];
        if($role_id < 0){

            //exception('请选择角色id!');
            $this->result([],'请选择角色id!',0);
        }else{
            $query[] = ['', 'exp', Db::raw("find_in_set({$role_id},role_ids)")];
        }



        $list = $this->model->where($query)->field('id,mobile,nickname as text,role_ids')->select()->toArray();


        $this->result($list);
    }





}
